package com.farmproduct.controller;

import com.farmproduct.pojo.Product;
import com.farmproduct.service.ProductService;
import com.farmproduct.tools.SnowflakeIdWorker;
import com.farmproduct.vo.ConditionSearch;
import com.farmproduct.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.NumberFormat;
import java.util.List;


@RestController
@RequestMapping(value="/product")
@CrossOrigin
public class ProductController {
    @Autowired
    private ProductService productService;

    //    根据用户添加产品
    @RequestMapping(value = "/add")
    public ResultVO addProductByUid(@RequestBody Product product){
        Long pid = SnowflakeIdWorker.generateId();
        product.setPid(pid.toString());
        if (productService.addProduct(product)){
                return new ResultVO(200,"success",0,pid.toString());
        }
        return new ResultVO();
    }

    //    根据用户获取产品
    @RequestMapping("/get/{uid}")
    public ResultVO getProductByUid(@PathVariable String uid){
        List<Product> productByUid = productService.getProductByUid(uid);
        if (productByUid!=null){
            return new ResultVO(200,"success",productByUid.size(),productByUid);
        }
        return new ResultVO();
    }

    //    根据pid获取产品
    @RequestMapping("/getproduct/{pid}")
    public ResultVO getProductByPid(@PathVariable String pid){
        Product product = productService.getProductByPid(pid);
        if (product!=null){
            return new ResultVO(200,"success",1,product);
        }
        return new ResultVO();
    }

    //条件获取产品对象
    @RequestMapping(value = "/getProductByCondition",method = RequestMethod.GET)
    public ResultVO getProductByCondition(@RequestParam("uid") String uid,@RequestParam(value = "pid") String pid,@RequestParam(value = "ptype") String ptype,@RequestParam(value = "destination") String destination){
        ConditionSearch conditionSearch = new ConditionSearch(uid, pid, ptype, destination);
        List<Product> productsByCondition = productService.getProductsByCondition(conditionSearch);
        if (productsByCondition!=null){
            return new ResultVO(200,"success",productsByCondition.size(),productsByCondition);
        }
        return new ResultVO();
    }



    //    删除多个商品
    @RequestMapping("/delProductList")
    public ResultVO delProductList(@RequestBody List<Product> productList){
        if (productService.delProductList(productList)){
            return new ResultVO(200,"success",0,null);
        }
        return new ResultVO();
    }

    //    删除商品
    @RequestMapping("/del")
    public ResultVO delProduct(@RequestBody Product product){
        if (productService.delProduct(product)){
            return new ResultVO(200,"success",0,null);
        }
        return new ResultVO();
    }
    //    更新商品
    @RequestMapping("/update")
    public ResultVO updateProduct(@RequestBody Product product){
        if (productService.updateProduct(product)){
            return new ResultVO(200,"success",0,null);
        }
        return new ResultVO();
    }
}

